package com.opdar.athena.web.controllers;

import com.opdar.athena.base.Constants;
import com.opdar.athena.base.RedisSessionManager;
import com.opdar.athena.entities.ErrorResult;
import com.opdar.athena.entities.UserEntity;
import com.opdar.athena.web.interceptors.AuthInterceptor;
import com.opdar.athena.web.service.MessageService;
import com.opdar.platform.annotations.Interceptor;
import com.opdar.platform.annotations.Request;
import com.opdar.platform.core.base.Context;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;

import java.sql.Timestamp;

/**
 * Created by shiju on 2017/7/2.
 */
@Controller
@Interceptor(AuthInterceptor.class)
public class MessageController {

    @Autowired
    private
    RedisSessionManager sessionManager;

    @Autowired
    private
    MessageService messageService;

    /**
     * @param source ip:port
     * @param destination ip:port
     * @return
     */
    @Request(value = "/message/migrate", format = Request.Format.JSON)
    public Object migrate(String source,String destination,Integer type ,Integer num,String migrateToken) {
        UserEntity user = (UserEntity) Context.getRequest().getAttribute("user");
        if(user.getRole().equals("ADMIN")){
            //判断是否有迁移权限
            if(migrateToken == null)migrateToken = "";
            if(num == null)num = 0;
            if(type == null)type = 1;
            messageService.migrate(source, destination,migrateToken,num,type);
            return true;
        }
        return false;
    }

    @Request(value = "/message/send", format = Request.Format.JSON)
    public Object send(String content,Integer type,String fakeId,String reciver,String sender) {
        if (StringUtils.isEmpty(reciver) || StringUtils.isEmpty(sender)){
            return new ErrorResult(Constants.ErrorCode.PARAMATER_IS_EMPTY);
        }
        return messageService.send(content, type, fakeId,sender,reciver);
    }

    @Request(value = "/message/notify", format = Request.Format.JSON)
    public Object notifyMsg(String messageId) {
        return messageService.notifyMsg(messageId);
    }

    @Request(value = "/message/find", format = Request.Format.JSON)
    public Object find(String sender, Timestamp ltTime,Timestamp gtTime) {
        UserEntity user = (UserEntity) Context.getRequest().getAttribute("user");
        return messageService.find(sender,user.getId(),ltTime,gtTime);
    }

    @Request(value = "/message/count/find", format = Request.Format.JSON)
    public Object findReadCount(String reciver) {
        UserEntity user = (UserEntity) Context.getRequest().getAttribute("user");
        return messageService.findReadCount(user.getId(),reciver);
    }

    @Request(value = "/message/count/update", format = Request.Format.JSON)
    public Object updateReadCount(String messageId) {
        UserEntity user = (UserEntity) Context.getRequest().getAttribute("user");
        return messageService.updateReadCount(messageId);
    }

}
